<template>
  <!-- popShow用来控制对话框的展示与否
       :show-close="false" 禁用原生的关闭按钮
-->
  <el-dialog
    title="编辑"
    v-model="popShow"
    width="30%"
    center
    :show-close="false"
  >
    <el-form :model="editData" ref="ruleFromRef">
      <el-form-item label="课程标题" prop="title" required>
        <el-input v-model="editData.title"></el-input>
      </el-form-item>

      <el-form-item label="课程价格" prop="price" required>
        <el-input v-model="editData.price"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="confirmClick('cancel')">取消</el-button>
        <el-button type="primary" @click="submitForm(ruleFromRef)"
          >确认</el-button
        >
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script setup>
import { defineProps, reactive, ref } from 'vue';
const { popShow, message, confirmClick } = defineProps([
  'popShow',
  'message',
  'confirmClick',
]);
// console.log(confirmClick);

/**
 * 编辑组件的维护
 */

const editData = reactive({
  title: message.title,
  price: message.price,
  id: message.id,
});

/**
 * 表单校验
 */

const ruleFromRef = ref(null); //ruleFromRef 初始化为空 将用于判断表单数据是否填写

const submitForm = async (formEl) => {
  if (!formEl) {
    // 未获得表单对象
    return;
  }
  await formEl.validate((valid, fields) => {
    if (valid) {
      // 数据填写正常
      confirmClick({
        title: editData.title,
        price: editData.price,
        id: editData.id,
      });
    } else {
      //数据填写不完整
      console.log('error', fields);
    }
  });
};
</script>
<style lang='less' scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}

:deep(.el-form-item__label) {
  width: 80px !important;
}

:deep(.el-form-item__content) {
  justify-content: center;
  margin-left: 0 !important;
}
</style>